using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.ObjectBuilder;
using GC2.ExceptionHandler;
using GC2.ExceptionHandler.Services;
using GC2.Entities.Admin;
using GC2.DataAccess;

namespace GC2.DataAccess.DAAdmin
{
    public class DAMovStock
    {
        private Database _database;        // Objeto DataBase del Data Access Application Block

        #region Propiedades miembro
        /// <summary>
        /// Seteo la propiedad _database de la clase.
        /// </summary>
        private Database DataBase
        {
            get { return _database; }
            set { _database = value; }
        }
        #endregion

        #region Constructor
        /// <summary>
        /// Contructor de la clase sin parametro. Crea un objeto DataBase
        /// del Data AppBlock tomando el defaultDatabase del app.config.
        /// </summary>
        //public AplicacionesDataAccess()
        //{
        //    this.DataBase = DatabaseFactory.CreateDatabase();
        //}
        #endregion

        #region DAMovStockGetNext
        public MovStock DAMovStockGetNext(string Connection, MovStock mov)
        {
            this.DataBase = DatabaseFactory.CreateDatabase(Connection);
            DbCommand cmd = DataBase.GetStoredProcCommand("MovStock_ProximoID");         

            try
            {
                using (cmd)
                {
                    DataBase.AddInParameter(cmd, "@Id_Rubro", DbType.Int16, mov.Id_Rubro);
                    DataBase.AddInParameter(cmd, "@Id_SubRubro", DbType.Int16, mov.Id_SubRubro);
                    DataBase.AddInParameter(cmd, "@Id_Articulo", DbType.String, mov.Id_Articulo);
                    DataBase.AddInParameter(cmd, "@Lote", DbType.String, mov.Lote);

                      IDataReader Odbreader = DataBase.ExecuteReader(cmd);
                      while (Odbreader.Read())
                      {
                          mov.Id_Mov = Convert.ToInt64(Odbreader["ProxId_Mov"]);
                      }
                      Odbreader.Close();
                      Odbreader.Dispose();
                }
            }
            catch (SqlException sql_ex)
            {
                ExceptionSQL exNew = new ExceptionSQL();
                throw new System.Exception(exNew.exSQL(sql_ex.ToString()).ToString());
            }
            catch (Exception ex)
            {
                ExceptionDAL exNew = new ExceptionDAL();
                throw new System.Exception(exNew.exDAL(ex.ToString()).ToString());
            }

            return mov;
        }
        #endregion

        #region DAMovStockInsert
        /// <summary>
        /// Inserta un registro en la tabla MovStock (por Articulo y Lote)
        /// </summary>
        /// <param name="Connection"></param>
        /// <param name="Stock"></param>
        public void DAMovStockInsert(string Connection, MovStock mov)
        {
            this.DataBase = DatabaseFactory.CreateDatabase(Connection);
            DbCommand cmd = DataBase.GetStoredProcCommand("MovStock_Insert");

            try
            {
                using (cmd)
                {
                    DataBase.AddInParameter(cmd, "@Id_Rubro", DbType.Int16, mov.Id_Rubro);
                    DataBase.AddInParameter(cmd, "@Rubro", DbType.String, mov.Rubro);
                    DataBase.AddInParameter(cmd, "@Id_SubRubro", DbType.Int16, mov.Id_SubRubro);
                    DataBase.AddInParameter(cmd, "@SubRubro", DbType.String, mov.SubRubro);
                    DataBase.AddInParameter(cmd, "@Id_Articulo", DbType.Int16, mov.Id_Articulo);
                    DataBase.AddInParameter(cmd, "@Articulo", DbType.String, mov.Articulo);
                    DataBase.AddInParameter(cmd, "@Lote", DbType.String, mov.Lote);
                    DataBase.AddInParameter(cmd, "@Id_Mov", DbType.Int64, mov.Id_Mov);
                    DataBase.AddInParameter(cmd, "@Operacion", DbType.Int16, mov.Operacion);
                    DataBase.AddInParameter(cmd, "@Id_Operacion", DbType.Int16, mov.Id_Operacion);
                    DataBase.AddInParameter(cmd, "@Fecha", DbType.DateTime, mov.Fecha);
                    DataBase.AddInParameter(cmd, "@Kilos", DbType.Decimal, mov.Kilos);
                    DataBase.AddInParameter(cmd, "@Saldo", DbType.Decimal, mov.Saldo);
                    DataBase.AddInParameter(cmd, "@ALote", DbType.Int16, mov.ALote);
                    DataBase.AddInParameter(cmd, "@Id_Usuario", DbType.Int16, mov.Id_Usuario);

                    DataBase.ExecuteNonQuery(cmd);
                }
            }
            catch (SqlException sql_ex)
            {
                ExceptionSQL exNew = new ExceptionSQL();
                throw new System.Exception(exNew.exSQL(sql_ex.ToString()).ToString());
            }
            catch (Exception ex)
            {
                ExceptionDAL exNew = new ExceptionDAL();
                throw new System.Exception(exNew.exDAL(ex.ToString()).ToString());
            }
        }
        #endregion
    }
}
